<?php
namespace App\Http\Controllers\Api;

use App\Models\WxIcon;
use App\Models\WxPage;
use Illuminate\Http\Request;

class PageController extends BaseController{

    /** tabs页面模板
     * @param Request $request
     */
    public function tabs(Request $request){

        global $__MINI_GLOBAL_PAYCODE_8__;
        if(!$__MINI_GLOBAL_PAYCODE_8__){
            return $this->fail(700000);
        }

        $id = $request->id;
        if(_empty_($id)){
            return $this->fail(200001);
        }
        $page = WxPage::find($id);

        if(_empty_($page)){
            return $this->fail(200003);
        }
        if(!in_array($page->type, [0, 1])){
            return $this->fail(200008);
        }
        $right = $page->right;
        $config = $page->config;

        $result = [
            'style' => $page->type,
            'header' => [
                'left' => [],
                'center' => $page->title,
                'right' => []
            ],
            'config' => [

            ]
        ];
        if($right){
            $right = json_decode($right, true);
            if($right){
                $result['header']['right'] = WxIcon::find($right[0]);
            }
        }

        $tabs = [];
        $tab_pages = [];
        if($config){
            $config_arr = json_decode($config, true);
            $i = 0;
            foreach ($config_arr as $tab){
                $tabs[] = [
                    'title' => $tab['name']
                ];
                $icon_ids = $tab['tab'];
                $tab_pages[$i] = [];
                if($icon_ids){
                    $icon_ids = explode(',', $icon_ids);
                    foreach ($icon_ids as $icon){
                        $icon_obj = WxIcon::find($icon);
                        $tab_pages[$i][] = $icon_obj;
                    }
                }
                $i++;
            }
        }

        $result['config']['tabs'] = $tabs;
        $result['config']['tab_pages'] = $tab_pages;
        return $this->success($result);

    }


    /** qrcodes页面模板
     * @param Request $request
     */
    public function qrcodes(Request $request){

        global $__MINI_GLOBAL_PAYCODE_8__;
        if(!$__MINI_GLOBAL_PAYCODE_8__){
            return $this->fail(700000);
        }

        $id = $request->id;
        if(_empty_($id)){
            return $this->fail(200001);
        }
        $page = WxPage::find($id);

        if(_empty_($page)){
            return $this->fail(200003);
        }
        if(!in_array($page->type, [3])){
            return $this->fail(200008);
        }
        $right = $page->right;
        $config = $page->config;

        $result = [
            'style' => $page->type,
            'header' => [
                'left' => [],
                'center' => $page->title,
                'right' => []
            ],
            'config' => [

            ]
        ];
        if($right){
            $right = json_decode($right, true);
            if($right){
                $result['header']['right'] = WxIcon::find($right[0]);
            }
        }

        $menus  = [];
        if($config){
            $config = json_decode($config, true);
            if($config['tabs']){
                if(is_string($config['tabs'])){
                    $config['tabs'] = json_decode($config['tabs'], true);
                }
            }else{
                $config['tabs'] = [];
            }
            $result['config'] = $config;
        }

        return $this->success($result);

    }

    /** menus页面模板
     * @param Request $request
     */
    public function menus(Request $request){

        global $__MINI_GLOBAL_PAYCODE_8__;
        if(!$__MINI_GLOBAL_PAYCODE_8__){
            return $this->fail(700000);
        }

        $id = $request->id;
        if(_empty_($id)){
            return $this->fail(200001);
        }
        $page = WxPage::find($id);

        if(_empty_($page)){
            return $this->fail(200003);
        }
        if(!in_array($page->type, [2])){
            return $this->fail(200008);
        }
        $right = $page->right;
        $config = $page->config;

        $result = [
            'style' => $page->type,
            'header' => [
                'left' => [],
                'center' => $page->title,
                'right' => []
            ],
            'config' => [

            ]
        ];
        if($right){
            $right = json_decode($right, true);
            if($right){
                $result['header']['right'] = WxIcon::find($right[0]);
            }
        }

        $menus  = [];
        if($config){
            $config_arr = explode(',', $config);
            $i = 0;
            foreach ($config_arr as $icon_id){
                $menus[] = WxIcon::find($icon_id);
                $i++;
            }
        }

        $result['config']['menus'] = $menus;
        return $this->success($result);

    }

}
